/**==============================================================================
            Copyright (c) 2013 Moritz Hader, Marcel Schrder
            All Rights Reserved.

	@file  ElementManager.java

	@brief Handles the ElementObjects and Markers and the classes to catch the 
	regarding informations from the IS

  This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
==============================================================================*/
package de.ibm.dhbw.backing.dataHolding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Logger;


/**
 * @author Marcel Schroeder - marcel_schroeder89@hotmail.com
 */
public class ElementManager {
	
	private static final Logger Log = Logger.getLogger(ElementManager.class.getCanonicalName());
	//----------- Properties and Constructor -----------//

	private ArrayList<ElementObject> elements;
	private MoeMessageObject mMO;
	private HashMap<Integer, Marker> markersMap;
	private IS_Connector connector;
	private Translator translator;
	
	private String connectionIp = "localhost";
	private String connectionPort = "8080";
	private float markerRadius = (float) 25.0;

	public ElementManager(String connectionIp, String connectionPort, float markerRadius) {
		Log.info("ElementManager created");
		
		this.elements = new ArrayList<ElementObject>();
		this.markersMap = new HashMap<Integer, Marker>();

		if(markerRadius != 0) {
			this.markerRadius = markerRadius;
		}
		
		this.translator = new Translator(this.markerRadius, this);
		
		this.setNewInfoSysLogdata(connectionIp, connectionPort);
	}
	
	
	//----------- Internal Methods -----------//
	
	//ready
	//successfully tested
	/**
	 * Sends a request to the IS to fetch Marker information and stores them as Marker objects in an ArrayList
	 */
	private void fetchMarkers() {
		String markersInJSON = this.connector.fetchMarkers();
		this.markersMap.clear();
		this.markersMap = translator.getMarkersOutOfJSONMap(markersInJSON);
		this.markersMap.put(Integer.parseInt("0"), new Marker(255, (float)0.0, (float)0.0));
	}
	
	//ready
	//successfully tested
	/**
	 * Sends a request to the IS to fetch information of ElementObject belonging to a list of Markers and stores them as ElementObject objects in this.elements
	 * @param markers An Array of Marker objects id in integer form to identify the Markers for which the ElementObject information should be requested
	 */
	private void fetchElements(int[] ids) {
	 	String markersInJSON = this.translator.getJSONOutOfMarkersID(ids);
	 	String elementsInJSON = this.connector.fetchElements(markersInJSON);
	 	ArrayList<ElementObject> newElements = translator.getElementsOutOfJSON(elementsInJSON);
	 	
	 	this.mMO = new MoeMessageObject(this.elements, newElements);
	 	
		this.elements.clear();
		this.elements = newElements;
	}
	
	
	//----------- Customised Getter and Setter -----------//

	public MoeMessageObject getElements(int[] markerIds) {
		this.fetchElements(markerIds);
		return this.mMO;
	}
	
	public void setNewInfoSysLogdata(String connectionIp, String connectionPort) {
		if(null != connectionIp && !connectionIp.isEmpty()) {
			this.connectionIp = connectionIp;
		}
		if(null != connectionPort && !connectionPort.isEmpty()) {
			this.connectionPort = connectionPort;
		}
		this.connector = new IS_Connector(this.connectionIp, this.connectionPort);
		
		this.fetchMarkers();
	}
	
	public void setMarkerRadius(float markerRadius) {
		if(markerRadius != 0) {
			this.markerRadius = markerRadius;
			this.translator.setMarkerRadius(this.markerRadius);
		}
	}
	
	public float getMarkerRadius() {
		return this.translator.getMarkerRadius();
	}
	
	
	//----------- Simple Getter and Setter -----------//

	public HashMap<Integer, Marker> getMarkersMap() {
		return markersMap;
	}
}
